// pages/main/sales/addconnections.js

Page({

    /**
     * 页面的初始数据
     */
    data: {
        form: {
            customername: getApp().globalData.userinfo.nickName
        },
        cname : "",
        addusers: [],
        url: getApp().globalData.apiurl,
        source: 0,
        loadingType: "more",
        totalCount: 0,
        totalPages: 0,
        pageSize: 10,
        pageIndex: 1,
        listData: [],
        phone:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let _this = this;
       
        _this.setData({
            cname : getApp().globalData.userinfo.nickName
        }),
        console.log(_this.cname)
    },

    myaddclick: function (e) {
        let phone = /^\d$/;
        let n = this;
        let fromdata = e.detail.value;

    
        fromdata.createordno = getApp().globalData.userinfo.nickName;
        console.log(fromdata)
        if (fromdata.customername == '' || fromdata.customername == undefined || fromdata.customername == null || fromdata.contactnumber == '' || fromdata.contactnumber == undefined || fromdata.contactnumber == null) {
            wx.showToast({
                title: '请填写客户名称和联系电话！',
                icon: 'none',
                duration: 3000,
            })
        }
        
        // else if(phone.test(fromdata.contactnumber)){
        //     wx.showToast({
        //         title: '请输入数字',
        //         icon: 'none',
        //         duration: 3000,
        //     })
        // }
        else {
            wx.request({
                url: n.data.url + "customer/customer/queryCustomerListByName",
                method: "GET",
                data: {
                    customerName: fromdata.customername,
                },
                success: function (t) {
                    console.log(t.data)
                    if (t.data.total > 0) {
                        setTimeout(function () {
                            wx.showToast({
                                title: '该客户已存在',
                                icon: "error",
                                duration: 1000,
                            })
                        }, 100)
                    } else {
                        // n.endSubmit(fromdata);
                        wx.request({
                            url: n.data.url + "customer/customer",
                            method: "POST",
                            data: fromdata,
                            success: function (t) {
                                console.log(t)
                                // console.log(n.users)
                                setTimeout(function(){
                                    wx.showToast({
                                        title: '添加成功',
                                        duration: 1000,
                                    });
                                },100)
                                
                            },
                            fail: function () {
                                wx.showToast({
                                    title: '新增用户信息失败，请稍后再试',
                                    icon: 'sucess',
                                    duration: 850,
                                });
                            },
                            complete: function () {
                                wx.redirectTo({
                                    url: "/pages/main/sales/connections",
                                });
                                console.log("请求完成")
                            }
                        })
                    }
                },
            })
        }
    },
    // endSubmit(fromdata) {
    //     console.log(fromdata)
        
    // },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})